Skip to Content

Counting Lines of Code with cloc

Sooner or later your project manager will approach you mumbling something about metrics, lines of code and accountability.

Much like tabs vs spaces there are people who are both for and against using lines of code as some sort of measurement. I won’t get into that argument but I will show you a fantastic tool to make this easy: cloc

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.

It’s available for Windows, Mac and Linux.

I also like it because CFML is one of it’s supported languages so it will parse: .cfm, .cfml and .cfc files.

Once installed running it is easy:

cloc .

This will run it in the current directory and should output something like this (using my blog as an example):

❯ cloc .
     427 text files.
     426 unique files.                                          
      14 files ignored.

github.com/AlDanial/cloc v 1.90  T=0.17 s (2480.8 files/s, 150715.5 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JSON                             7              0              0           8815
Markdown                       191           2705              0           6497
TOML                            26            782             22           1905
Sass                            56            329             22           1583
HTML                            77            226              0           1341
JavaScript                      24            111             54            553
CSS                              5              2              7            105
YAML                             4              7             18             73
SVG                             25              0              0             55
-------------------------------------------------------------------------------
SUM:                           415           4162            123          20927
-------------------------------------------------------------------------------

This is pretty but cloc is also very powerful and has lots of really useful options. View the help available for all of the details but some of the highlights:

  • various output formats are available: –csv, –json, –md, –yaml
  • –sql - Write results as SQL create and insert statements which can be read by a database program such as SQLite.

You can also pass a git hash to compare lines of code between commits

❯ cloc --git-diff-all 5fcff 1a9b8
     423 text files.
     423 text files.
      26 files ignored.                                         

github.com/AlDanial/cloc v 1.90  T=0.58 s (711.4 files/s, 42320.5 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Markdown
 same                          188           2598              0           6013
 modified                        1              0              0              8
 added                           0              0              0              0
 removed                         0              3              0              4
-------------------------------------------------------------------------------
SUM:
 same                          411           4052            123          20321
 modified                        1              0              0              8
 added                           0              0              0              0
 removed                         0              3              0              4
-------------------------------------------------------------------------------

cloc is a great utility for your toolbox. You may not use it every day but if your manager asks your for the TPS report - you can quickly generate something useful.